package com.chixing.fourteen.service.impl;

import com.chixing.fourteen.pojo.CustomerOrder;
import com.chixing.fourteen.pojo.Message;
import com.chixing.fourteen.repository.CustomerOrderRepository;
import com.chixing.fourteen.service.CustomerOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Y2014188432
 */
@Service
public class CustomerOrderServiceImpl implements CustomerOrderService {

    @Autowired
    private CustomerOrderRepository customerOrderRepository;

    @Override
    /**
     * 创建一个新的订单
     */
    public int createOrder(int id,String startPlace, String endPlace, String remake,double length) {
        CustomerOrder customerOrder = new CustomerOrder();
        customerOrder.setCustomerId(id);
        customerOrder.setCustomerOrderStatus(1);
        customerOrder.setCustomerOrderCreateTime(new Date());
        customerOrder.setCustomerOrderStartPlace(startPlace);
        customerOrder.setCustomerOrderEndPlace(endPlace);
        customerOrder.setCustomerOrderPrice(length);
        customerOrder.setCustomerOrderRemakes(remake);
        System.out.println(customerOrder);
        return customerOrderRepository.insert(customerOrder);
    }

    @Override
    /**
     * 根据用户 id 查询所有订单（进行分页操作，按时间降序排列）
     */
    public List<CustomerOrder> findAll(int id,int page) {
        page = (page - 1) * 5;
        return customerOrderRepository.select(id,page,5);
    }

    @Override
    /**
     * 改变订单状态
     */
    public int changeStatus(int id, int status) {
        return customerOrderRepository.update(id, status);
    }

    @Override
    /**
     * 根据用户 id 获取订单总数
     */
    public int getCustomerOrderCount(int customerId) {
        return customerOrderRepository.countByCustomerId(customerId);
    }

    @Override
    /**
     * 获取订单的完整信息
     */
    public Message getMessage(int customerOrderId) {
        return customerOrderRepository.getMessage(customerOrderId);
    }
}
